package com.rongshu.view.submitArticle.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.view.base.AdapterListener;
import com.rongshu.view.submitArticle.bean.LabelBean;

import java.util.List;


/**
 * created by zhengweis on 2018/11/1.
 * description：投稿设置-标签列表
 */
public class SubmitAiticleLabelAdapter extends RecyclerView.Adapter<SubmitAiticleLabelAdapter.ViewHolder> {
    private Context context;
    private List<LabelBean> list;
    private AdapterListener listener;

    public SubmitAiticleLabelAdapter(Context context, List<LabelBean> list) {
        this.list = list;
        this.context = context;
    }

    public void setAdapterListener(AdapterListener listener){
        this.listener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.adapter_label_rectangle, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder viewHolder, final int i) {
        viewHolder.tag.setBackgroundResource(R.drawable.shape_oval_gray_background);
        viewHolder.tag.setText(list.get(i).getTitle());
        viewHolder.tag.setBackgroundResource(list.get(i).getIsSelected() ?
                R.drawable.shape_oval_green_background : R.drawable.shape_oval_gray_background);
        viewHolder.tag.setTextColor(list.get(i).getIsSelected() ?
                ContextCompat.getColor(context, R.color.color_3CC051) : ContextCompat.getColor(context, R.color.color_4a4a4a));
        viewHolder.tag.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                for (LabelBean bean : list) {
                    bean.setIsSelected(false);
                }
                list.get(i).setIsSelected(true);
                listener.callBack(i);
                notifyDataSetChanged();
            }
        });
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tag;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tag = itemView.findViewById(R.id.search_tag);
        }
    }
}
